package com.j.lemon.learn.thread.threadPool;

import java.util.concurrent.*;

public class TraceThreadPoolExecutor extends ThreadPoolExecutor {
    public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    public void execute(Runnable command) {
        super.execute(wrap(command,clientTrace(),Thread.currentThread().getName()));
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(task);
    }
    private Exception clientTrace(){
        return new Exception("Client stack trace");
    }
    private Runnable wrap(final Runnable task,final Exception clientStack,String clientThreadName){
        return new Runnable() {
            @Override
            public void run() {
                try{
                    task.run();
                }catch (Exception e){
                    clientStack.printStackTrace();
                    throw e;
                }
            }
        };
    }

    public static void main(String[] args) {
        //ThreadPoolExecutor executor = new TraceThreadPoolExecutor(5,5,0L,TimeUnit.SECONDS, new LinkedBlockingQueue());
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for(int i =0;i<5;i++){
            executor.execute(new DivTask(100,i));
        }
        DivTask task = new DivTask(100,2);
        Thread t = new Thread(task);

    }
}

class DivTask implements Runnable{
    public int a ;
    public int b;
    public DivTask(int a ,int b){
        this.a=a;
        this.b=b;
    }

    @Override
    public void run() {
        System.out.println(a/b);
    }
}
